<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 安装主程序
// +----------------------------------------------------------------------

namespace app\admin\command;

use app\api\model\AccessTokenModel;
use app\api\model\DouYinService;
use app\api\model\Order;
use app\common\model\ItemModel;
use app\common\model\DouyinModel;
use app\common\model\SourceModel;
use PDO;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\facade\Log;
use think\facade\Request;
use think\facade\View;
use util\File;
use util\Random;

class Lock extends Command
{
    /**
     * @var \think\Request Request 实例
     */
    protected $request;

    protected function configure()
    {
        $this->setName('lock')
            ->setDescription('Command Close');
    }

    /**
     * 命令行安装
     */
    protected function execute(Input $input, Output $output)
    {
        echo "开始执行" . "\n";
        try {
            //查询未发布的视频
            $item = (new ItemModel())->where(['status' => 0])->field('id,create_time,sid')->select();
            $lockTime = (new \app\admin\model\Config())->where(['name' => 'lock'])->value('value') * 60;
            foreach ($item as $key => $val) {
                $time = time() - strtotime($val['create_time']);
                //获取视频的状态
                $source = SourceModel::get($val['sid']);
                if (($lockTime < $time) && $source['is_lock'] == 1) {
                    $source->is_lock = 0;
                    $source->save();
                }
                if((time() - strtotime($val['create_time'])) > $lockTime + 3600 ){
                    //删除未发布的视频
                    (new ItemModel())->where(['id' => $val['id']])->delete();
                    echo $val['id'] . "\n";
                }
            }
            echo "执行成功" . "\n";
        } catch (Exception $e) {
            Log::mylog('lock', $e->getMessage(), 'lock');
            die;

        }

    }
}
